package com.heer.gymnasium.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.heer.api.ResponseData.ApiResponse;
import com.heer.gymnasium.AppHelper;
import com.heer.gymnasium.R;
import com.heer.model.MyMsgBean;
import com.heer.model.OkBean;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2017/8/2.
 */

public class MessageAdapter extends ListAdapter<MyMsgBean> {

    LayoutInflater inflater;
    protected AppHelper mAppHelper;

    public MessageAdapter(Activity activity) {
        super(activity);
        inflater=LayoutInflater.from(activity);
        mAppHelper=AppHelper.context();
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        final MyMsgBean item=getItem(position);
        if (view==null)
        {
            view=inflater.inflate(R.layout.item_my_messge,null);
            holder=new ViewHolder();
            holder.iv_title= (ImageView) view.findViewById(R.id.iv_messge_title);
            holder.ll_title= (RelativeLayout) view.findViewById(R.id.ll_messge_title);
            holder.tv_context= (TextView) view.findViewById(R.id.tv_messge_content);
            holder.tv_title= (TextView) view.findViewById(R.id.tv_messge_title);
            holder.iv_hine= (ImageView) view.findViewById(R.id.iv_message_hine);
            view.setTag(holder);
        }else {
            holder= (ViewHolder) view.getTag();
        }
        holder.tv_title.setText(item.getTitle());
        holder.tv_context.setText(item.getContent());
        holder.tv_context.setVisibility(View.GONE);
        if (item.getIsread().equals("0"))
        {
            holder.iv_hine.setVisibility(View.VISIBLE);
        }else {
            holder.iv_hine.setVisibility(View.GONE);
        }

        final Boolean[] open = {false};
        holder.ll_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.iv_hine.setVisibility(View.GONE);
                if (open[0])
                {
                    open[0] =false;
                    holder.tv_context.setVisibility(View.GONE);

                    holder.iv_title.setImageDrawable(mActivity.getResources().getDrawable(R.mipmap.arrow_right));

                }else {
                    open[0]=true;
                    holder.tv_context.setVisibility(View.VISIBLE);
                    holder.iv_title.setImageDrawable(mActivity.getResources().getDrawable(R.mipmap.arrow_down));
                    if (item.getIsread().equals("0"))
                    {
                        readMsg(item.getId());
                    }
                }
            }
        });
        return view;
    }

    public class ViewHolder
    {
        public RelativeLayout ll_title;
        public TextView tv_title,tv_context;
        public ImageView iv_title;
        public ImageView iv_hine;
    }

    public void readMsg(String msgId)
    {
        mAppHelper.apiService().ReadMsg(msgId).enqueue(new Callback<ApiResponse<OkBean>>() {
            @Override
            public void onResponse(Call<ApiResponse<OkBean>> call, Response<ApiResponse<OkBean>> response) {

            }

            @Override
            public void onFailure(Call<ApiResponse<OkBean>> call, Throwable t) {

            }
        });
    }
}
